///  jh_asset_picker.dart
///
///  Created by iotjin on 2022/09/10.
///  description: 基于微信UI的图片/视频选择器(支持拍照及录制视频) 封装wechat_assets_picker、wechat_camera_picker

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';


// 最大数量
const int _maxAssets = 9;
// 录制视频最长时长, 默认为 15 秒，可以使用 `null` 来设置无限制的视频录制
const Duration _maximumRecordingDuration = Duration(seconds: 15);
// 一行显示几个
const int _lineCount = 3;
// 每个GridView item间距(GridView四周与内部item间距在此统一设置)
const double _itemSpace = 5.0;
// 右上角删除按钮大小
const double _deleteBtnWH = 30.0;
// 默认添加图片
const String _addBtnIcon = 'images/selectPhoto_add.png';
// 默认删除按钮图片
const String _deleteBtnIcon = 'images/selectPhoto_close.png';
// 默认背景色
const Color _bgColor = Colors.transparent;

enum AssetTypes {
  image,
  video,
  imageAndVideo,
}

enum CallBackType {
  delete,
  add,
}

class JhAssetPicker extends StatefulWidget {
  const JhAssetPicker({
    Key? key,
    this.assetType = AssetTypes.image,
    this.maxAssets = _maxAssets,
    this.lineCount = _lineCount,
    this.itemSpace = _itemSpace,
    this.maximumRecordingDuration = _maximumRecordingDuration,
    this.bgColor = _bgColor,
    this.callBack,
    this.selectedAssets,
  }) : super(key: key);

  final AssetTypes assetType; // 资源类型
  final int maxAssets; // 最大数量
  final int lineCount; // 一行显示几个
  final double itemSpace; // 每个GridView item间距(GridView四周与内部item间距在此统一设置)
  final Duration? maximumRecordingDuration; // 录制视频最长时长, 默认为 15 秒，可以使用 `null` 来设置无限制的视频录制
  final Color bgColor; // 背景色
  final Function(List<AssetEntity> assetEntityList,CallBackType type)? callBack; // 选择回调
  final selectedAssets;

  @override
  State<JhAssetPicker> createState() => _JhAssetPickerState();
}

class _JhAssetPickerState extends State<JhAssetPicker> {
  List<AssetEntity> _selectedAssets = [];
  final Color _themeColor = const Color(0xff0ba4fc);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _selectedAssets = widget.selectedAssets ?? [];
  }

  @override
  Widget build(BuildContext context) {
    return _body();
  }

  _body() {

    var allCount = _selectedAssets.length + 1;

    return Container(
      color: widget.bgColor,
      child: GridView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          // 可以直接指定每行（列）显示多少个Item
          crossAxisCount: widget.lineCount, // 一行的Widget数量
          crossAxisSpacing: widget.itemSpace, // 水平间距
          mainAxisSpacing: widget.itemSpace, // 垂直间距
          childAspectRatio: 1.0, // 子Widget宽高比例
        ),
        // GridView内边距
        padding: EdgeInsets.all(widget.itemSpace),
        itemCount: _selectedAssets.length == widget.maxAssets ? _selectedAssets.length : allCount,
        itemBuilder: (context, index) {
          if (_selectedAssets.length == widget.maxAssets) {
            return _itemWidget(index);
          }
          if (index == allCount - 1) {
            return _addBtnWidget();
          } else {
            return _itemWidget(index);
          }
        },
      ),
    );
  }

  // 添加按钮
  Widget _addBtnWidget() {
    return GestureDetector(
      child: const Image(image: AssetImage(_addBtnIcon)),
      onTap: () => _showBottomSheet(),
    );
  }

  // 图片和删除按钮
  Widget _itemWidget(index) {
    return GestureDetector(
      child: Container(
        color: Colors.transparent,
        child: Stack(
          alignment: Alignment.topRight,
          children: <Widget>[
            ConstrainedBox(
              constraints: const BoxConstraints.expand(),
              child: _loadAsset(_selectedAssets[index]),
            ),
            GestureDetector(
              child: const Image(
                image: AssetImage(_deleteBtnIcon),
                width: _deleteBtnWH,
                height: _deleteBtnWH,
              ),
              onTap: () => _deleteAsset(index),
            )
          ],
        ),
      ),
      onTap: () => _clickAsset(index),
    );
  }

  Widget _loadAsset(AssetEntity asset) {

    if(asset.type == AssetType.video) {
      return Stack(
        children: [

          Positioned.fill(child:Image(image: AssetEntityImageProvider(asset), fit: BoxFit.cover)),
          ColoredBox(
            color: Theme
                .of(context)
                .dividerColor
                .withOpacity(0.3),
            child: const Center(
              child: Icon(
                Icons.video_library,
                color: Colors.white,
                size: 26.0,
              ),
            ),
          ),
        ],
      );
    }
    return Image(image: AssetEntityImageProvider(asset), fit: BoxFit.cover);
  }

  void _deleteAsset(index) {
    setState(() {
      _selectedAssets.removeAt(index);
      // 选择回调
      widget.callBack?.call(_selectedAssets,CallBackType.delete);
    });
  }

  // 全屏查看
  void _clickAsset(index) {
    AssetPickerViewer.pushToViewer(
      context,
      currentIndex: index,
      previewAssets: _selectedAssets,
      themeData: AssetPicker.themeData(_themeColor),
    );
  }

  // 点击添加按钮
  void _showBottomSheet() {
    showModalBottomSheet(
        context: context,
        isDismissible: true,
        isScrollControlled: false,
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(15), topRight: Radius.circular(15))),
        builder: (BuildContext context) {
          return SizedBox(
              height: ScreenUtil().setHeight(280.0),
              child: Column(
                children: [
                  Expanded(
                    child: InkWell(
                        onTap: () {
                          _openCamera(context);
                        },
                        child: Column(
                          children: [
                            Expanded(
                              child: Container(
                                alignment: Alignment.center,
                                child: Text(
                                  '拍摄',
                                  style: TextStyle(
                                      fontWeight: FontWeight.w600,
                                      fontSize: ScreenUtil().setSp(28)),
                                ),
                              ),
                            ),
                            // Expanded(
                            //     child: Container(
                            //       alignment: Alignment.topCenter,
                            //       child: Text(
                            //         '照片或视频',
                            //         style: TextStyle(
                            //             color: Colors.black54,
                            //             fontWeight: FontWeight.w600,
                            //             fontSize: ScreenUtil().setSp(30)),
                            //       ),
                            //     ))
                          ],
                        )),
                  ),
                  Container(
                    height: 0.5,
                    color: Colors.black12,
                  ),
                  Expanded(
                    child: InkWell(
                        onTap: () {
                          _openAlbum(context);
                        },
                        child: Column(
                          children: [
                            Expanded(
                              child: Container(
                                alignment: Alignment.center,
                                child: Text(
                                  '相册',
                                  style: TextStyle(
                                      fontWeight: FontWeight.w600,
                                      fontSize: ScreenUtil().setSp(28)),
                                ),
                              ),
                            ),
                            // Expanded(
                            //     child: Container(
                            //       alignment: Alignment.topCenter,
                            //       child: Text(
                            //         '视频不超过120秒',
                            //         style: TextStyle(
                            //             color: Colors.black54,
                            //             fontWeight: FontWeight.w600,
                            //             fontSize: ScreenUtil().setSp(30)),
                            //       ),
                            //     ))
                          ],
                        )),
                  ),
                  Container(
                    height: 10,
                    color: const Color(0xffeeeeee),
                  ),
                  Expanded(
                      child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: Center(
                            child: Text(
                              '取消',
                              style: TextStyle(
                                  fontWeight: FontWeight.w600,color: Colors.black45,
                                  fontSize: ScreenUtil().setSp(28)),
                            ),
                          )))
                ],
              ));
        });

  }

  // 相册选择
  Future<void> _openAlbum(context) async {
    if (!Platform.isAndroid && !Platform.isIOS) {

      Fluttertoast.showToast(msg:'当前平台暂不支持');
      return;
    }

    RequestType requestType = RequestType.image;
    if (widget.assetType == AssetTypes.video) {
      requestType = RequestType.video;
    }
    if (widget.assetType == AssetTypes.imageAndVideo) {
      requestType = RequestType.common;
    }
    final List<AssetEntity>? result = await AssetPicker.pickAssets(
      context,
      pickerConfig: AssetPickerConfig(
        maxAssets: widget.maxAssets,
        requestType: requestType,
        selectedAssets: _selectedAssets,
        themeColor: _themeColor,
        // textDelegate: const EnglishAssetPickerTextDelegate(),
      ),
    );
    if (result != null) {
      setState(() {
        _selectedAssets = result;
      });
      // 相册选择回调
      widget.callBack?.call(result,CallBackType.add);
    }
  }

  // 拍照或录像
  Future<void> _openCamera(context) async {
    if (!Platform.isAndroid && !Platform.isIOS) {
      Fluttertoast.showToast(msg:'当前平台暂不支持');
      return;
    }
    final AssetEntity? result = await CameraPicker.pickFromCamera(
      context,

      pickerConfig: CameraPickerConfig(
        // 是否可以录像
        enableRecording: widget.assetType != AssetTypes.image,
        // 录制视频最长时长
        maximumRecordingDuration:  Duration(seconds: 15),//widget.maximumRecordingDuration,
        // textDelegate: const EnglishCameraPickerTextDelegate(),
      ),
    );
    if (result != null) {
      setState(() {
        _selectedAssets.add(result);
        // 相机回调
        widget.callBack?.call(_selectedAssets,CallBackType.add);
      });
    }
  }
}
